import { BrowserWindow } from 'electron'

/**
 * 实现窗口放大、缩小等功能
 * @param ipcMain
 */
export function useTitleEvents(ipcMain: Electron.IpcMain) {
  ipcMain.on('minimize-window', () => {
    const win = BrowserWindow.getFocusedWindow()
    if (win) win.minimize()
  })

  ipcMain.on('toggle-maximize', () => {
    const win = BrowserWindow.getFocusedWindow()
    if (win) {
      if (win.isMaximized()) {
        win.unmaximize()
        win.webContents.send('window-unmaximize') // 通知渲染进程窗口已还原
      } else {
        win.maximize()
        win.webContents.send('window-maximize') // 通知渲染进程窗口已最大化
      }
    }
  })

  ipcMain.on('close-window', () => {
    const win = BrowserWindow.getFocusedWindow()
    if (win) win.close()
  })

  ipcMain.handle('get-maximize-status', () => {
    const win = BrowserWindow.getFocusedWindow()
    return win?.isMaximized() || false
  })
}
